var app = getApp();

Page({
    data: {
        navH: app.globalData.navHeight,
        zone: "A区",
        page: 1,
        loading: !1,
        lastPage: !1,
        goodsList: !1,
        plusH: 81,
        cont_id: 0,
        scrollLeft: 0,
        scrollLefts: 0,
        share_id: 0,
        goods_type: 0,
        cates: [ {
            key: "A",
            name: "3元礼金"
        }, {
            key: "B",
            name: "5元礼金"
        }, {
            key: "C",
            name: "礼金三"
        }, {
            key: "D",
            name: "礼金四"
        }, {
            key: "E",
            name: "礼金五"
        }, {
            key: "F",
            name: "礼金六"
        }, {
            key: "G",
            name: "礼金七"
        }, {
            key: "H",
            name: "礼金八"
        } ],
        reg: 0,
        cids: "1,9,10,12",
        catess: [ {
            name: "服饰内衣",
            cids: "1,9,10,12"
        }, {
            name: "居家生活",
            cids: "4,14"
        }, {
            name: "母婴美妆",
            cids: "2,3"
        }, {
            name: "食品家装",
            cids: "6,14"
        }, {
            name: "运动鞋包",
            cids: "5,11,13"
        }, {
            name: "3C家电虚拟",
            cids: "7,8"
        } ]
    },
    loginAfter: function(a) {
        app.loginAfter(a);
    },
    loginCancel: function() {
        app.loginCancel();
    },
    onChangecont: function(a) {
        var t, o = this, e = a.currentTarget.dataset.cont_id;
        o.data.cont_id != e && (t = 1 < e ? 120 * e : 10 * e, o.setData({
            cont_id: e,
            scrollLeft: t,
            scrollLefts: 0,
            cids: "1,9,10,12"
        }), o.data.zone = a.currentTarget.dataset.key + "区", o.data.page = 1, o.data.lastPage = !1, 
        o.getGiftGoods());
    },
    navBack: function() {
        app.globalData.from_act ? app.goIndex() : wx.navigateBack();
    },
    onChangeregion: function(a) {
        var t = this, o = a.currentTarget.dataset.reg, e = a.currentTarget.dataset.cids, a = 0, a = 2 < o ? 30 * o : 10 * o;
        t.setData({
            reg: o,
            cids: e,
            scrollLefts: a
        }), t.data.page = 1, t.data.lastPage = !1, t.getGiftGoods();
    },
    onLoad: function(a) {
        var e = this;
        app.util.request({
            url: "entry/wxapp/Setting",
            data: {
                name: "jd_gift_tip,jd_gift_zone",
                page: "jdgift"
            },
            success: function(a) {
                if (a = a.data.data, e.setData(a), 1 == e.data.show_gift_cates && e.setData({
                    plusH: e.data.plusH + 40
                }), (a = e.data.jd_gift_zone) && 0 < (a = a.split(",")).length) {
                    var t, o = [];
                    for (t in a) {
                        if (7 < t) break;
                        o[t] = {
                            key: e.data.cates[t].key,
                            name: e.data.cates[t].name
                        };
                    }
                    e.setData({
                        cates: o
                    });
                }
                e.getGiftGoods();
            }
        });
    },
    getGiftGoods: function() {
        var t = 0 < arguments.length && void 0 !== arguments[0] && arguments[0], o = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : "cover", e = this;
        app.util.request({
            url: "entry/wxapp/GetJdGift",
            data: {
                page: e.data.page,
                zone: e.data.zone,
                cids: e.data.cids
            },
            success: function(a) {
                if ("function" == typeof t && t(), "append" == o) return a.data.data.length < 1 ? void (e.data.lastPage = !0) : (e.data.goodsList = 1 == e.data.page ? a.data.data : e.data.goodsList.concat(a.data.data), 
                void e.setData({
                    goodsList: e.data.goodsList
                }));
                1 == e.data.page && wx.pageScrollTo({
                    scrollTop: 0,
                    duration: 0
                }), e.setData({
                    goodsList: a.data.data
                });
            }
        });
    },
    getGiftPoster: function(a) {
        var o, e, s, t, n, i;
        app.globalData.userInfo ? (o = this, e = a.currentTarget.dataset.gdx, s = o.data.goodsList[e].goods_name, 
        t = o.data.goodsList[e].goods_id, a = o.data.goodsList[e].giftCouponKey, n = o.data.goodsList[e].price_subsidy, 
        i = o.data.goodsList[e].picurl, wx.showLoading({
            title: "加载中...",
            mask: !0
        }), app.util.request({
            url: "entry/wxapp/JdGiftLink",
            showLoading: !1,
            data: {
                goods_id: t,
                giftcouponKey: a
            },
            success: function(a) {
                var t;
                "less" != a.data.message ? (t = a.data.data, app.util.request({
                    url: "entry/wxapp/GetGiftPoster",
                    showLoading: !1,
                    data: {
                        qr_url: t,
                        banner: i,
                        title: s,
                        price: n
                    },
                    success: function(a) {
                        a = a.data.data, wx.getImageInfo({
                            src: a,
                            success: function(a) {
                                wx.saveImageToPhotosAlbum({
                                    filePath: a.path,
                                    success: function(a) {
                                        wx.showToast({
                                            title: "保存至相册成功",
                                            icon: "success"
                                        });
                                    },
                                    fail: function(a) {
                                        console.log(a), wx.showModal({
                                            title: "系统提示",
                                            content: "请转至“授权设置”页面打开“保存到相册”开关",
                                            confirmColor: "#f43f6c",
                                            confirmText: "去打开",
                                            showCancel: !1,
                                            success: function(a) {
                                                wx.openSetting({
                                                    withSubscriptions: !0,
                                                    success: function() {}
                                                });
                                            }
                                        });
                                    }
                                });
                            },
                            complete: function() {
                                o.delPoster(), wx.hideLoading();
                            }
                        });
                    },
                    complete: function() {
                        wx.hideLoading();
                    }
                })) : (t = o.data.goodsList[e].tlj_send_url, wx.showToast({
                    title: "今日礼金已抢完，每天0点更新",
                    icon: "none",
                    duration: 3e3
                }));
            },
            complete: function() {
                wx.hideLoading();
            }
        })) : app.checkLogin();
    },
    delPoster: function() {
        app.util.request({
            url: "entry/wxapp/delTempPoster",
            showLoading: !1,
            success: function(a) {
                console.log(a);
            }
        });
    },
    goTop: function(a) {
        wx.pageScrollTo ? (this.setData({
            show_goTop: !1
        }), wx.pageScrollTo({
            scrollTop: 0,
            duration: 300
        })) : wx.showToast({
            title: "暂不支持"
        });
    },
    copyGiftLink: function(a) {
        var o, e, t, s, n, i;
        app.globalData.userInfo ? (o = this, e = a.currentTarget.dataset.gdx, t = o.data.goodsList[e].goods_id, 
        a = o.data.goodsList[e].giftCouponKey, s = o.data.goodsList[e].goods_name, n = o.data.goodsList[e].tlj_amount, 
        i = o.data.goodsList[e].price_subsidy, app.util.request({
            url: "entry/wxapp/JdGiftLink",
            data: {
                goods_id: t,
                giftcouponKey: a
            },
            success: function(a) {
                var t;
                "less" != a.data.message ? (t = a.data.data, wx.setClipboardData ? wx.setClipboardData({
                    data: s + "\n礼金补贴：" + n + "元\n补贴价：" + i + "\n" + t,
                    success: function(a) {
                        wx.showToast({
                            title: "复制成功"
                        });
                    }
                }) : console.log("当前微信版本不支持setClipboardData")) : (t = o.data.goodsList[e].tlj_send_url, 
                wx.showToast({
                    title: "今日礼金已抢完，每天0点更新",
                    icon: "none",
                    duration: 3e3
                }));
            }
        })) : app.checkLogin();
    },
    toJdGift: function(a) {
        var t;
        app.globalData.userInfo ? (t = a.currentTarget.dataset.gdx, a = this.data.goodsList[t].goods_id, 
        t = this.data.goodsList[t].giftCouponKey, app.util.request({
            url: "entry/wxapp/JdGiftLink",
            data: {
                goods_id: a,
                giftcouponKey: t
            },
            success: function(a) {
                "less" == a.data.message ? wx.showToast({
                    title: "今日礼金已抢完，每天0点更新",
                    icon: "none",
                    duration: 3e3
                }) : wx.navigateToMiniProgram({
                    appId: "wxca1fe42a16552094",
                    path: "/pages/union/proxy/proxy?spreadUrl=" + encodeURIComponent(a.data.data),
                    envVersion: "release",
                    success: function(a) {
                        console.log("打开小程序成功");
                    }
                });
            }
        })) : app.checkLogin();
    },
    onReady: function() {},
    onShow: function() {
        app.reUserInfo();
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {
        this.data.page = 1, this.data.lastPage = !1, this.getGiftGoods(function() {
            wx.stopPullDownRefresh();
        });
    },
    onReachBottom: function() {
        if (!0 === this.data.lastPage) return !1;
        this.data.page++, this.getGiftGoods("", "append");
    },
    onShareTimeline: function() {
        var a = void 0 !== app.globalData.userInfo.uid ? "from_uid=" + app.globalData.userInfo.uid + "&from_act=share" : "";
        return {
            title: "京享礼金，限量福利，好货一元就购~~~",
            imageUrl: this.data.goodsList[0].picurl,
            query: a
        };
    },
    onShareAppMessage: function(a) {
        var t, o = "button" == a.from && void 0 !== a.target.dataset.gdx ? (o = a.target.dataset.gdx, 
        t = "“京享礼金”👉︎" + this.data.goodsList[o].goods_name.substr(0, 20), this.data.goodsList[o].picurl) : (t = "京享礼金，限量福利，好货一元就购~~~", 
        this.data.goodsList[0].picurl);
        return {
            title: t,
            path: "/first_duoduoke/pages/index/jdgift" + (void 0 !== app.globalData.userInfo.uid ? "?from_uid=" + app.globalData.userInfo.uid + "&from_act=share" : "?from_act=share"),
            imageUrl: o,
            success: function(a) {
                "shareAppMessage:ok" == a.errMsg ? wx.showToast({
                    title: "分享成功",
                    icon: "success"
                }) : wx.showToast({
                    title: "分享失败",
                    icon: "none"
                });
            },
            fail: function(a) {
                "shareAppMessage:fail cancel" == a.errMsg || a.errMsg, wx.showToast({
                    title: "分享失败",
                    icon: "none"
                });
            }
        };
    }
});